(function($)
{
    $.fn.loadContacts=function(params)
    {
    	params = $.extend({
    	url: "http://...", 
    	action : "GETLIST", 
    	caller : "SALESFORCES", 
    	lang : "FR",
    	idClient : ""}, params); // Merge des paramètres
    	params.url = params.url + "&action=" + params.action + "&caller=" + params.caller;
	    cont = $.getJSON(params.url, function(result) {
			cont = result.data;
		});
		// Temporaire
	   var cont = {"content_type": "application/json", "status": "SUCCESS", "messages": [], "data": {
"salesforce": [
	{
	"id_cli": "",
	"nom_cli": "", 
	"desc_cli": "", 
	"pStat_cli": "", 
	"adr_cli": "", 
	"autre_info_client": "", 
	"autre_info_client": "", 
	"ca_history": [
		{"ca_date": "2012/01/01", "ca_value": "131233.00", "waerk": ""},
		{"ca_date": "2014/01/01", "ca_value": "344534.00", "waerk": ""}, 
		{"ca_date": "2010/02/01", "ca_value": "123130.00", "waerk": ""},
		{"ca_date": "2012/08/01", "ca_value": "347456.00", "waerk": ""},
		{"ca_date": "2012/07/01", "ca_value": "131233.00", "waerk": ""},
		{"ca_date": "2014/03/01", "ca_value": "344534.00", "waerk": ""}, 
		{"ca_date": "2010/10/01", "ca_value": "123130.00", "waerk": ""},
		{"ca_date": "2012/01/01", "ca_value": "347456.00", "waerk": ""},  
		{"ca_date": "2013/02/01", "ca_value": "123139.00", "waerk": ""}],
	"orders_history": [
		{"ref_ord_cli": "xxx", "date_ord_cli": "10.12.2012", "ord_prod": "13123.00", "ord_qty_prod": "5", "ord_price_prod": "25"}, 
		{"ref_ord_cli": "xxx", "date_ord_cli": "10.23.2134", "ord_prod": "13123.00", "ord_qty_prod": "20", "ord_price_prod": "34"}],
	"offers_history": [
		{"ref_offers_cli": "yyy", "date_offers_cli": "10.12.2012", "offers_prod": "13123.00", "offers_qty_prod": "5", "offers_price_prod": "25"},
		{"ref_offers_cli": "yyy", "date_offers_cli": "10.12.2012", "offers_prod": "13123.00", "offers_qty_prod": "5", "offers_price_prod": "25"},
		{"ref_offers_cli": "yyy", "date_offers_cli": "10.12.2012", "offers_prod": "13123.00", "offers_qty_prod": "5", "offers_price_prod": "25"},
		{"ref_offers_cli": "yyy", "date_offers_cli": "10.12.2012", "offers_prod": "13123.00", "offers_qty_prod": "5", "offers_price_prod": "25"},
	 	{"ref_offers_cli": "yyy", "date_offers_cli": "10.12.2012", "offers_prod": "13123.00", "offers_qty_prod": "5", "offers_price_prod": "25"},
		{"ref_offers_cli": "yyy", "date_offers_cli": "10.12.2012", "offers_prod": "13123.00", "offers_qty_prod": "5", "offers_price_prod": "25"},
		{"ref_offers_cli": "yyy", "date_offers_cli": "10.12.2012", "offers_prod": "13123.00", "offers_qty_prod": "5", "offers_price_prod": "25"},
		{"ref_offers_cli": "yyy", "date_offers_cli": "10.12.2012", "offers_prod": "13123.00", "offers_qty_prod": "5", "offers_price_prod": "25"},
		{"ref_offers_cli": "yyy", "date_offers_cli": "10.23.2134", "offers_prod": "13123.00", "offers_qty_prod": "20", "offers_price_prod": "34"}]
	}
]}};
	
   		  var dataCa = new google.visualization.DataTable();
   		  var minDate = new Date(1900, 1 ,1);
	      var maxDate = new Date (2100, 1, 1);
	      var c = 0;
	      
	      dataCa.addColumn('date', 'Date');
	      dataCa.addColumn('number', 'Chiffre d affaire : ');
	      for (var x in cont.data.salesforce[c].ca_history){
	      	if(new Date(cont.data.salesforce[c].ca_history[x].ca_date) > minDate){
	      		minDate = new Date(cont.data.salesforce[c].ca_history[x].ca_date);
	      	}
	      	if(new Date(cont.data.salesforce[c].ca_history[x].ca_date) < maxDate){
	      		maxDate = new Date(cont.data.salesforce[c].ca_history[x].ca_date);
	      	}
	      	dataCa.addRow([new Date(cont.data.salesforce[c].ca_history[x].ca_date), parseFloat(cont.data.salesforce[c].ca_history[x].ca_value)]);
      	  }
	      google.setOnLoadCallback(drawMyCharts(dataCa, maxDate, minDate));      	    
      	
      	   // datatable offers history
      	   $('#dynamicGauche').html( '<table cellpadding="0" width="400" cellspacing="0" border="0" class="display" id="example"></table>' );
      	   var aDataSet = new Array();  
      	   for (var i in cont.data.salesforce[c].orders_history){
		  	   var tmpArr = new Array(cont.data.salesforce[c].orders_history[i].ref_ord_cli, cont.data.salesforce[c].orders_history[i].date_ord_cli,
		  	   	 cont.data.salesforce[c].orders_history[i].ord_prod, cont.data.salesforce[c].orders_history[i].ord_qty_prod, 
		  	   	 cont.data.salesforce[c].orders_history[i].ord_price_prod);
			   aDataSet.push(tmpArr);
      	   }
      	   
      	    oTable = $('#example').dataTable({
					"aaData": aDataSet,
					"aoColumns": [
						{ "sTitle": "Client references" },
						{ "sTitle": "Order Date" },
						{ "sTitle": "Products" },
						{ "sTitle": "Qty", "sClass": "center" },
						{ "sTitle": "Price", "sClass": "center" }
					],
					"bJQueryUI": true,
        			"sPaginationType": "full_numbers"
		   });
		   
      	   $('#dynamicDroite').html( '<table cellpadding="0" width="400" cellspacing="0" border="0" class="display" id="example2"></table>' );	
      	   var aDataSet = new Array();  
      	   for (var i in cont.data.salesforce[c].offers_history){
		  	   var tmpArr = new Array(cont.data.salesforce[c].offers_history[i].ref_offers_cli, cont.data.salesforce[c].offers_history[i].date_offers_cli,
		  	   	 cont.data.salesforce[c].offers_history[i].offers_prod, cont.data.salesforce[c].offers_history[i].offers_qty_prod, 
		  	   	 cont.data.salesforce[c].offers_history[i].offers_price_prod);
			   aDataSet.push(tmpArr);
      	   }
		   $('#example2').dataTable( {
					"aaData": aDataSet,
					"aoColumns": [
						{ "sTitle": "Client references" },
						{ "sTitle": "Order Date" },
						{ "sTitle": "Products" },
						{ "sTitle": "Qty", "sClass": "center" },
						{ "sTitle": "Price", "sClass": "center" }
					],
					"bJQueryUI": true,
        			"sPaginationType": "full_numbers"
		  });	
				
      	
      	
      	return this.each(function()
       	{
       	
			
        });
         	                
    };
})(jQuery);


function drawMyCharts(data, minDate, maxDate){
	  var annotatedtimeline = new google.visualization.AnnotatedTimeLine(
      document.getElementById('visualization'));
      annotatedtimeline.draw(data, {
                                    //'allValuesSuffix': '%', // A suffix that is added to all values
                                    'colors': ['#aaa', 'red', '#1797C0'], // The colors to be used
                                    'displayAnnotations': true,
                                    'displayExactValues': true, // Do not truncate values (i.e. using K suffix)
                                    'displayRangeSelector' : false, // Do not sow the range selector
                                    'displayZoomButtons': false, // DO not display the zoom buttons
                                    'fill': 70, // Fill the area below the lines with 20% opacity
                                    'legendPosition': 'newRow', // Can be sameRow
                                    //'max': 100000, // Override the automatic default
                                    //'min':  100000, // Override the automatic default
                                    'scaleColumns': [0, 1], // Have two scales, by the first and second lines
                                    'scaleType': 'allfixed', // See docs...
                                    'thickness': 1, // Make the lines thicker
                                    'zoomStartTime': minDate, //NOTE: month 1 = Feb (javascript to blame)
                                    'zoomEndTime': maxDate //NOTE: month 1 = Feb (javascript to blame)
                                   });
}


